<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
    <style type="text/css">
        div {
            width: 100px;
            height: 100px;
            border: 1px solid silver;
        }
    </style>
    <script>
        //定义全局变量，用于记录秒数
        window.onload = function () {
            var oBtn = document.getElementsByTagName("input");
            var oDiv = document.getElementsByTagName("div")[0];

            var color = ["red", "orange", "yellow", "green", "blue", "purple"];

            var timer = null;

            var i = 1;
            oBtn[0].onclick = function () {
                clearInterval(timer);

                timer = setInterval(function () {
                    oDiv.style.backgroundColor = color[i];
                    i++;
                    //可以让图片无限循环  非常实用
                    i = i % color.length;
                },500)
            };
            oBtn[1].onclick = function () {

                clearInterval(timer);
            };
        }
    </script>
</head>
<body>
<input type="button" value="开始"/>
<input type="button" value="暂停"/>
<div></div>
</body>
</html>